package com.testmanager.testmananger.shuiyou.strCompare.service;

import com.testmanager.testmananger.foundation.dto.PageableDto;
import com.testmanager.testmananger.foundation.mapper.PageBaseMapper;
import com.testmanager.testmananger.foundation.util.FlakeIdGenerator;
import com.testmanager.testmananger.shuiyou.strCompare.domain.*;
import com.testmanager.testmananger.shuiyou.strCompare.dto.CompareScriptDto;
import com.testmanager.testmananger.shuiyou.strCompare.dto.CompareTasksSearchDto;
import com.testmanager.testmananger.shuiyou.strCompare.dto.SyCompareTaskDto;
import com.testmanager.testmananger.shuiyou.strCompare.mapper.CompareScriptMappre;
import com.testmanager.testmananger.shuiyou.strCompare.mapper.CompareTaskMapper;
import com.testmanager.testmananger.shuiyou.strCompare.mapper.SyProjectMapper;
import com.testmanager.testmananger.shuiyou.strCompare.respository.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SqlCompareTaskService {
    @Autowired
    SyCompareTaskRepository syCompareTaskRepository;
    @Autowired
    SyCompareTaskCustom syCompareTaskCustom;
    @Autowired
    SyProjectRepository projectRepository;
    @Autowired
    SyDbManagerRepository dbManagerRepository;
    @Autowired
    SySqlCompareRepository scriptRepository;
    @Autowired
    SyTaskVariableValueRepository variableValueRepository;

    @Autowired
    SyCompareScriptKeyRepository scriptKeyRepository;// 1.锚点名   2.变量名

    @Autowired
    ExceuteTaskService exceuteTaskService;

    @Autowired
    FlakeIdGenerator flakeIdGenerator;

    public ResponseEntity searchDetail(CompareTasksSearchDto searchDto){
        log.info("SqlCompareTaskService searchDetail:{}",searchDto);
        Page<SyCompareTaskEntity> search = syCompareTaskCustom.search(searchDto);
        List<SyCompareTaskEntity> taskEntities = search.getContent();
        List<SyCompareTaskDto> taskDtos = taskEntities.stream().parallel().map(t -> {

            SyProjectEntity project = projectRepository.findById(t.getProjectId());
            SySqlCompareScriptEntity script = scriptRepository.findByScriptno(t.getScriptId());


                List<SyCompareScriptKeyEntity> authors = scriptKeyRepository.findAllByScriptIdAndType(script.getScriptno(), 1);
                List<SyCompareScriptKeyEntity> variablenames = scriptKeyRepository.findAllByScriptIdAndType(script.getScriptno(), 2);
            CompareScriptDto compareScriptDto = CompareScriptMappre.toCompareScriptDto(script, variablenames, authors);


            SyDbManagerEntity srcDb = dbManagerRepository.findByDbno(t.getSrcDbID());
            SyDbManagerEntity destDb = dbManagerRepository.findByDbno(t.getDestDbId());
            List<SyTaskVariableValueEntity> variables = variableValueRepository.findAllByTaskId(t.getTaskno());

            return CompareTaskMapper.toSyCompareTaskDto(t, compareScriptDto, SyProjectMapper.toSyprojectDto(project,new ArrayList<>()), srcDb, destDb,variables);
        }).collect(Collectors.toList());

        PageableDto pageableDto = PageBaseMapper.pageExchange(search, taskDtos);
        return ResponseEntity.ok(pageableDto);
    }


    public ResponseEntity search(CompareTasksSearchDto searchDto){
        log.info("SqlCompareTaskService search:{}",searchDto);
        Page<SyCompareTaskEntity> search = syCompareTaskCustom.search(searchDto);
        List<SyCompareTaskEntity> taskEntities = search.getContent();


        PageableDto pageableDto = PageBaseMapper.pageExchange(search, taskEntities);
        return ResponseEntity.ok(pageableDto);
    }


    public ResponseEntity save(SyCompareTaskDto taskDto){
        log.info("saveo SyCompareTaskDto:{}",taskDto);

        if (null == taskDto.getTaskno()){
            String taskno = "ta"+ flakeIdGenerator.newId();
            taskDto.setTaskno(taskno);
            log.info("雪花算法，id:{}",taskno);
        }
        if (0 == taskDto.getId()){

            taskDto.setId(flakeIdGenerator.newId());

        }
        SyCompareTaskEntity taskEntity = CompareTaskMapper.toTaskEntity(taskDto);

        SyCompareTaskEntity save = syCompareTaskRepository.save(taskEntity);
        List<SyTaskVariableValueEntity> variables = taskDto.getVariables();
        variables.stream().forEach(t->t.setTaskId(save.getTaskno()));
        variableValueRepository.saveAll(variables);
         exceuteTaskService.execute(save.getId());
        return ResponseEntity.ok(save);
    }

    public SyCompareTaskEntity saveTask(SyCompareTaskDto taskDto){
        log.info("saveo SyCompareTaskDto:{}",taskDto);

        if (null == taskDto.getTaskno()){
            taskDto.setTaskno("ta"+ new FlakeIdGenerator(02).newId());
        }
        if (0 == taskDto.getId()){

            taskDto.setId(flakeIdGenerator.newId());

        }
        SyCompareTaskEntity taskEntity = CompareTaskMapper.toTaskEntity(taskDto);

        SyCompareTaskEntity save = syCompareTaskRepository.save(taskEntity);
        List<SyTaskVariableValueEntity> variables = taskDto.getVariables();
        variables.stream().forEach(t->t.setTaskId(save.getTaskno()));
        variableValueRepository.saveAll(variables);
         exceuteTaskService.execute(save.getId());
        return save;
    }

    public ResponseEntity update(SyCompareTaskDto taskDto){
        log.info("update taskDto:{}",taskDto);
        if (null == taskDto.getTaskno()){
            taskDto.setTaskno("ta"+ new FlakeIdGenerator(02).newId());
        }
        if (0 == taskDto.getId()){

            taskDto.setId(flakeIdGenerator.newId());

        }
        SyCompareTaskEntity taskEntity = CompareTaskMapper.toTaskEntity(taskDto);
        SyCompareTaskEntity save = syCompareTaskRepository.save(taskEntity);
        List<SyTaskVariableValueEntity> variables = taskDto.getVariables();
        variables.stream().forEach(t->t.setTaskId(save.getTaskno()));
        variableValueRepository.saveAll(variables);
        return ResponseEntity.ok("success");
    }

    /**
     * 批量任务执行
     * @param taskDtos
     * @return
     */
    public ResponseEntity batchTask(List<SyCompareTaskDto>  taskDtos){

        log.info("batchTask:{}",taskDtos);
        List tasks = new ArrayList();
        taskDtos.stream().parallel().forEach(t->{
            t.setName(t.getScript().getName()+"任务");
            t.setScriptName(t.getScript().getName());
            ResponseEntity save = save(t);
            Object body = save.getBody();
            tasks.add(body);
        });

        return  ResponseEntity.ok(tasks);

    }

    @Transactional
    public ResponseEntity deleteTask(String taskno){

        log.info("deletask:{}",taskno);
        syCompareTaskRepository.deleteAllByTaskno(taskno);
        variableValueRepository.deleteAllByTaskId(taskno);


        return ResponseEntity.ok("success");
    }






   // public
}
